package com.ccl.iot.template;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.widget.CheckBox;

import com.ccl.iot.R;

public class ImageCheckView extends CheckBox {
	private void initButtonStyle(){
		super.setSingleLine();
		//super.setPadding(0, 0, 0, 0);
		super.setBackgroundResource(R.drawable.imagecheckbox);
	}
	
	public ImageCheckView(Context context) {
		super(context);
		
		initButtonStyle();
	}

	public ImageCheckView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
		initButtonStyle();
	}

	public ImageCheckView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		
		initButtonStyle();
	}
	
	public void setCheckDrawable(Drawable aUncheck, Drawable aChecked){
		try{
			int tLeft = super.getPaddingLeft();
			int tRight = super.getPaddingRight();
			int tTop = super.getPaddingTop();
			int tBottom = super.getPaddingBottom();
			int tWidth = 0;
			int tHeight = 0;
			
			StateListDrawable tCheck = new StateListDrawable();
			
			tCheck.addState(new int[]{-android.R.attr.state_checked}, aUncheck);
			tCheck.addState(new int[]{android.R.attr.state_checked}, aChecked);
			
			if (tLeft != 0 || tRight != 0){
				if (tTop != 0 || tBottom != 0){
					tHeight = aUncheck != null ? aUncheck.getMinimumHeight() : (aChecked != null ? aChecked.getMinimumHeight() : 0);
				}
				
				tWidth = aUncheck != null ? aUncheck.getMinimumWidth() : (aChecked != null ? aChecked.getMinimumWidth() : 0);
			}else if (tTop != 0 || tBottom != 0){
				tHeight = aUncheck != null ? aUncheck.getMinimumHeight() : (aChecked != null ? aChecked.getMinimumHeight() : 0);
			}
			
			if (tWidth != 0 || tHeight != 0){
				super.setPadding(tWidth, tHeight, 0, 0);
			}

			super.setButtonDrawable(tCheck);
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	
	public void setCheckDrawable(int aUncheckResID, int aCheckedResID){
		try{
			setCheckDrawable(super.getResources().getDrawable(aUncheckResID), super.getResources().getDrawable(aCheckedResID));
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	
	public void setCheckBitmap(Bitmap aUncheck, Bitmap aChecked){
		try{
			setCheckDrawable(new BitmapDrawable(super.getResources(), aUncheck), new BitmapDrawable(super.getResources(), aChecked));
		}catch (Exception e){
			e.printStackTrace();
		}
	}
}
